#include "SoundManager.h"

#include "Application.h"

#if defined(TARGET_IPHONE_SIMULATOR) || defined(TARGET_OS_IPHONE)
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <al.h>
#include <alc.h>
#endif

////////////////////////////////////////////////////////////////////////
SoundManager::SoundManager() :
m_sfx(new CSfx[gameloft::audio::NUM_SFX]),
m_music(new CSfx[gameloft::audio::NUM_MUSIC]),
m_globalVolumeGain(1.0f)
{
	ALCdevice *alDevice = alcOpenDevice(NULL);
	ALCcontext *alContext = alcCreateContext(alDevice, NULL);	
	alcMakeContextCurrent(alContext);

	Application::GetInstance()->RegisterForUpdate(this);
}

////////////////////////////////////////////////////////////////////////
SoundManager::~SoundManager()
{
	SAFE_DEL_ARRAY(m_sfx);
	SAFE_DEL_ARRAY(m_music);

	ALCcontext *pContext = alcGetCurrentContext();
	ALCdevice *pDevice = alcGetContextsDevice(pContext);;

	alcMakeContextCurrent(NULL);
	alcDestroyContext(pContext);
	alcCloseDevice(pDevice);
}

////////////////////////////////////////////////////////////////////////
void 
SoundManager::playSfx(int sfxId, bool isStatic, bool loop)
{
	m_sfx[sfxId].Init(AudioFilenames::getInstance().getSfxFilename(sfxId), isStatic);

	//if(isStatic)
	//{ m_sfx[sfxId].ReadWholeBuffer(); }

	m_sfx[sfxId].Play(loop?-1:1);
}

////////////////////////////////////////////////////////////////////////
void
SoundManager::stopSfx(int sfxId)
{
	m_sfx[sfxId].Stop();
}

////////////////////////////////////////////////////////////////////////
void
SoundManager::stopAllSfx()
{
	for(int i=0; i<gameloft::audio::NUM_SFX; i++)
	{ m_sfx[i].Stop(); }
}

////////////////////////////////////////////////////////////////////////
void 
SoundManager::playMusic(int musicId, bool isStatic, bool loop)
{
	m_music[musicId].Init(AudioFilenames::getInstance().getMusicFilename(musicId));

	if(isStatic)
	{ m_music[musicId].ReadWholeBuffer(); }

	m_music[musicId].Play(loop?-1:1);
}

////////////////////////////////////////////////////////////////////////
void
SoundManager::stopMusic(int musicId)
{
	m_music[musicId].Stop();
}

////////////////////////////////////////////////////////////////////////
void
SoundManager::stopAllMusic()
{
	for(int i=0; i<gameloft::audio::NUM_MUSIC; i++)
	{ m_music[i].Stop(); }
}

////////////////////////////////////////////////////////////////////////
CTrackStereo*
SoundManager::getTrack(gameloft::audio::SfxFile track)
{
	m_sfx[track].Init(AudioFilenames::getInstance().getSfxFilename(track));
	return m_sfx[track].getTrack();
}

////////////////////////////////////////////////////////////////////////
void
SoundManager::update(TimeStamp timestamp)
{
	// update any streaming audio
	for(int i=0; i<gameloft::audio::NUM_SFX; i++)
	{
		if(!m_sfx[i].isStatic())
		{ m_sfx[i].Update(); }
	}

	for(int i=0; i<gameloft::audio::NUM_MUSIC; i++)
	{
		if(!m_music[i].isStatic())
		{ m_music[i].Update(); }
	}
}

////////////////////////////////////////////////////////////////////////
void
SoundManager::setListenerPos(const irr::core::vector3df &pos,
                             const irr::core::vector3df &front,
                             const irr::core::vector3df &up
                             ) const
{
	float vec[6];
	vec[0] = front.X;
	vec[1] = front.Y;
	vec[2] = front.Z;
	vec[3] = up.X;
	vec[4] = up.Y;
	vec[5] = up.Z;

	// set the position and orientation of the listener
	alListener3f(AL_POSITION, pos.X, pos.Y, pos.Z);
	alListenerfv(AL_ORIENTATION, vec);
}

////////////////////////////////////////////////////////////////////////
